<?php

defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractX4youController');

class AbstractUserController extends AbstractX4youController {

	/**
	 * The model object of the whole members/models/advertiser
	 * @var UserModel
	 */
	var $_model;

	function __construct() {
		parent::__construct();
		
		RClassesImporter::cls('models.UserModel');
		$this->_model = new UserModel();
	}
	
	/**
	 * Abstract function for register
	 * @abstract
	 * @param UserDTO $dto
	 * @param String $msg
	 * @return unknown_type
	 */
	function register($dto=null, $msg='') {
		echo "Abtract register";
	}
	
	/**
	 * The general processing the register for all users
	 * @return UserDTO or bool false if wrong and in this case the controller is set redirect to the register function with error msg
	 */
	function processReg($groupId = 3, $groupCode = 'user', $capChatValid = true, $saveUser = true) {
		if (SysRequest::getMethod() != 'POST') {
			$this->setRedirect(SYS_ROOT_URL);
			return false;
		}
		
		RClassesImporter::cls('dbo.dtos.UserDTO');
		$dto = new UserDTO();
		$dto->bind($_POST);
		
		if ($capChatValid) {
			// Validate capcha
			$captCtrl = &$this->__getCaptchaCtrl();
			if (!$captCtrl->isValid()) {
				$this->register($dto, SysDBLanguage::_('Invalid security code!'));
				return false;
			}
		}
		// Generate activation code for all users
		$dto->user_group_code 	= $groupCode;
		$dto->user_group_id		= $groupId;
		$dto->activation_code 	= RStringHelper::generateRandom(32);
		
		$model = $this->_model;
		if ($saveUser) {
			$model->saveDTO($dto);
			if ($dto->hasError()) {
				$this->register($dto, $dto->getAllErrorsMsg());
				return false;
			}
		}
		$_SESSION['__newuser'] = serialize($dto); 
		return $dto;
	}
	
	/**
	 * General function for case all information of registration is ok.
	 */
	function registration_suceesfull() {
		if ($_SESSION['__newuser']) {
			RClassesImporter::cls('dbo.dtos.UserDTO');
			$dto = unserialize($_SESSION['__newuser']);
			unset($_SESSION['__newuser']);
			
			// prepare email for activate this user:
			// Send the email for activation
			$userName = base64_encode($dto->user_name);
			$activationCode = base64_encode($dto->activation_code);
			$activeLink = SystemSite::urlRoute("?app=users&act=active&activateCode={$activationCode}&ref={$userName}");
		  
			eval('$mailBody ="'.SysApplication::loadTemplate('front/mail.register').'";');
			
			RCoreImporter::cls('helper.RMailHelper');
			if (RMailHelper::sentMail($dto->email, "Activation for new account.", $mailBody)) {
				require SysApplication::getTemplatePath('front/thank-you');
			}
			else {
				require SysApplication::getTemplatePath('front/mail-failed');
			}
		}
		else {
			$this->setRedirect(SYS_ROOT_URL);
		}
	}
	
	/**
	 * Set a redirect to a function
	 *
	 * @access	public
	 * @param	string URL to redirect to.
	 * @return	void
	 * 
	 */
	function setRedirectToFunction($function, $msg = '') {
		if ($msg) {
			$msg = base64_encode($msg);
		}
		
		return parent::setRedirectToFunction($function, $msg);
	}
	
	/**
	 * Get the msg content.
	 * @return string
	 */
	function _getMsg() {
		$msg = SysRequest::getVar('msg');
		if ($msg) {
			$msg = urlencode($msg);
			$msg = base64_decode($msg);
			$msg = SysDBLanguage::_($msg);
		}
		return $msg;
	}
}